import { PipeTransform, Injectable, BadRequestException } from '@nestjs/common';
import { isUUID } from 'class-validator';

@Injectable()
export class ParseUUIDPipe implements PipeTransform<string, string> {
  transform(value: string): string {
    if (value === undefined || value === null || value === '') {
      throw new BadRequestException('UUID is required');
    }

    if (!isUUID(value)) {
      throw new BadRequestException('Value must be a valid UUID');
    }

    return value;
  }
}

@Injectable()
export class ParseOptionalUUIDPipe
  implements PipeTransform<string, string | undefined>
{
  constructor(private readonly defaultValue?: string) {}

  transform(value: string): string | undefined {
    if (value === undefined || value === null || value === '') {
      return this.defaultValue;
    }

    if (!isUUID(value)) {
      throw new BadRequestException('Value must be a valid UUID');
    }

    return value;
  }
}
